ssh-keygen -t ed25519 -C "your_email@example.com"Alice Byers, Data Innovation Team
Scottish Government
These slides aim to demonstrate the use of Git and GitHub in the Scottish Government when working in the SCOTS network.
An understanding of the concepts of version control, Git and GitHub is assumed. More information can be found in the Introduction to Version Control slides.
Further guidance and resources for using version control on SCOTS are available on the Statistics Group sharepoint site.
Make an iFix request for “Git version 2.21.0.windows.1”
Git is a free and open-source software and therefore does not incur a cost to install
Git Bash is a command line interface for using Git
It is installed with Git
Some basic commands include:
pwd; prints present working directory
ls; lists files contained in working directory
cd <filepath>; changes working directory
ONS Learning Hub has further training on Command Line Basics
Your name and email is linked to every commit you make using Git
If you plan to use GitHub, make sure your user name and email address match those associated with your GitHub account
To do this, run the following in Git Bash (replacing my details with your own):
Install Git via iFix
Set name and email address in Git Bash
Open Git Bash from Windows start menu
Use cd command to change directory to your working folder
git init to initiate Git. This will create a .git folder within your project. (Note: you only need to do this once per project.)The gitignore defines what files should not be tracked by Git
This is especially important if you plan on using GitHub as sensitive information should not be made available there
Generally, the following should be ignored:
Data files
Passwords or credentials
Code that contains sensitive information
Configuration files
To tell Git to ignore these files:
Create a new file in your directory called .gitignore. This can be done in the usual way in File Explorer, or by using the touch command in Git Bash.
Open the gitignore file in a text editor (or R) and add names of folders and files to be ignored.
git status to show a summary of your Git repository - run this often to check that your other git commands have done what you expect them to dodata.csv is not listed here. This is because we have told Git to ignore csv files in the gitignore.Use git add to ‘stage’ files for the next commit
Either list the files you’d like to stage; e.g. git add code.R, or
To stage all tracked files, use a full stop; e.g. git add .
git status to check that the correct files have been stagedUse git commit to commit the files to the Git history
All staged files will be included in the commit
Accompany the commit with a message to give a short and informative description of the changes
git status again shows that there are no further changes to commitgit log --oneline will give a short summary of the commit historycode.R. Add some commented lines to give the script a title and description.git status to check that Git has recognised the changegit diff to inspect what changes have been made to code.R. Green text highlights additions and red text highlights deletions.git add and git commit to stage and commit the change to code.R.git log to view the Git history - there are now two commitsCommit often (especially when you’re still learning)
Write commit messages that make sense (your future self and colleagues will thank you)
Run git status often (especially when you’re still learning)
Check which files have changes tracked
Check you have staged the correct files
Check files that should be ignored are not being tracked (and committed!)
Make a change
git add
git commit
git status often
You can also interact with Git from within RStudio (instead of using Git Bash)
Not all Git functionality is available, but it can be more user-friendly and convenient if you’re working with R
Changes are listed in the Git pane (usually in the top right window)
Like Git Bash, you need to both stage and commit the change
To stage, tick the box next to each file you’d like to add (top-left pane)
To commit, enter a message and click ‘Commit’ (top-right pane)
Go to github.com and click ‘Sign Up’
Make your gov.scot email address public
Add your employer
Add a photo (if you’d like!)
Work projects should all be hosted from the Data Science Scotland organisation
Easier to find related repositories
Easier to manage who can access repositories
When people leave the organisation, code doesn’t leave with them!
There’s lots of useful information in the welcome repository
Request to join the organisation
Once your request has been accepted, you will be able to create Git repositories in Data Science Scotland
Select Data Science Scotland as owner for work projects
Give the repository a name
Choose whether to make your repository public or private
Add a README file
Click the green ‘Create repository’ button
A ‘remote’ is a version of your Git repository hosted on the internet or network somewhere
This should be thought of as the main place where your repository is stored
Most commonly, GitHub is used to host remote repositories. But, it could also be a folder on an internal shared network.
Users take a copy (‘clone’) of the repository from the remote
Users regularly ‘push’ their changes back to the remote so other users have access to the latest version
Users regularly ‘pull’ from the remote to ensure they are working with the latest version
An SSH key is a way of identifying yourself to GitHub that means you don’t have to provide your username and password every time
You must use an SSH key when working with GitHub on SCOTS
To set up, generate an SSH key pair in Git Bash and add the public key to your GitHub account
You only need to create an SSH key once per device.
Generate an SSH key in Git Bash
(Use the email registered with your Github account)
When asked where you want to create the key, press enter to use the default location
When asked if you want to set a passphrase, press enter twice to skip
Copy the SSH key:
Copy the returned value from Git Bash.
Add the copied SSH key to your GitHub account by going to Settings, then SSH and GPG keys
Now you have an SSH key set up, you should use the SSH URL to clone repositories from GitHub
Click the green ‘Code’ button and under ‘Clone’, select ‘SSH’, and copy the address
Open Git Bash and navigate to the directory you’d like to clone the repository to
Use git clone <url> to clone the repository
Change directory to the cloned repository using cd
Follow Git workflow to:
Add a new file
git add
git commit
‘Push’ the commit to GitHub (the remote) using git push
Note that git status now says the repository is up to date with origin/main (this is another name for the remote repository)
‘Pull’ from GitHub regularly using git pull to ensure your local copy of the repository is up to date (especially if other people are also working on the repository)
Note that there is now an extra commit when running git log
Use SSH keys to connect to GitHub from Git Bash
git clone to clone a repository from GitHub
Follow Git workflow described earlier in slides
git push often to push local commits to GitHub
git pull often to pull new commits on GitHub to local copy/clone
When you create a Git repository, you begin with a single ‘branch’, usually called main
The main branch is usually viewed as the ‘production-ready’ version of your code
You can create new branches to make developments, test new ideas and to facilitate multiple people working on the code at the same time
Branches allow users to make changes to files without affecting the ‘production-ready’ main branch
Changes made in a branch can be peer reviewed before being merged into the main branch
More information on branching in the Duck book
Alice Byers
Reproducible Analytical Pipeline (RAP) Developer
Data Innovation Team, Scottish Government